import Controller from '../../decorators/controller'
import get from '../../decorators/get'
import post from '../../decorators/post'
import put from '../../decorators/put'
import BaseController from '../base'
import ProductTypeDao from '../../dao/productTypeDao'
import ProductOptionDao from "../../dao/productOptionDao";

@Controller('/api/admin/productTypes')
export default class extends BaseController {
    @get("/")
    list() {
        var params = this.getParams()
        // this.ctx.body = "hello world"
        return ProductTypeDao.where(params).page({
            from: params.from,
            limit: params.size
        }).select()
    }

    @get("/count")
    count() {
        var params = this.getParams()
        // this.ctx.body = "hello world"
        return ProductTypeDao.where(params).count()
    }

    @post("/")
    async add() {
        var payload = this.getBody()
        var currentUser = await this.getCurrentUser()
        return await ProductTypeDao.add({
            clientId: currentUser.clientId,
            ...payload
        })
    }

    @get("/detail")
    async one() {
        var {id} = this.getParams()
        var results = await ProductTypeDao.where({id})
            .leftJoin("productOption",
                '"productOption"."id"="productType"."typeId"',
                '"productOption".name as productOptionName').select()
        return results[0]
    }

    @get("/select")
    async select() {
        return await ProductTypeDao.where().select()
    }

    @put("")
    async put() {
        var {id} = this.getParams()
        var body = this.getBody()
        var results = await ProductTypeDao.update(id, body)
        return results
    }

}


